/*
 ============================================================================
 Name		: CFaceGraph.cpp
 Author	  : lenli
 Version	 : 1.0
 Copyright   : 2008
 Description : CFaceGraph implementation
 ============================================================================
 */

#include "FaceGraph.h"
#include <s32file.h> //kniznica pre pracu so subormi - potrebna kniznica efsrv.lib
#include <BITDEV.H> //for drawing on bitmap 
#include <bautils.h> //for BaflUtils, link against: bafl.lib
#include <COEMAIN.H> //for CCoeEnv, link against: cone.lib
#include "ImageUtils.h"

CFaceGraph::CFaceGraph()
	{
	// No implementation required
	}

CFaceGraph::~CFaceGraph()
	{
	}

CFaceGraph* CFaceGraph::NewLC()
	{
	CFaceGraph* self = new (ELeave) CFaceGraph();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CFaceGraph* CFaceGraph::NewL()
	{
	CFaceGraph* self = CFaceGraph::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CFaceGraph::ConstructL()
	{
	}

void CFaceGraph::LoadFromFile(const TDesC& aFileName)
	{
			//before opening file we need a fileserver connection
			RFs fileserver;
			User::LeaveIfError(fileserver.Connect());
			CleanupClosePushL(fileserver);
		 
			RFileReadStream readStream;
			readStream.PushL();// reader on cleanup stack
			User::LeaveIfError(readStream.Open(fileserver, aFileName, EFileRead));
			
			TInt x;
			TInt y;
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			up = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			up1_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			up1_right = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			up2_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			up2_right = TPoint(x, y);
			    
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right = TPoint(x, y);		  
			
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			down = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			down1_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			down1_right = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			down2_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			down2_right = TPoint(x, y);
			    
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			nose_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			nose_right = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left_eye_center = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left_eye_up = TPoint(x, y);
			    
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left_eye_down = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left_eye_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left_eye_right = TPoint(x, y);

			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right_eye_center = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right_eye_up = TPoint(x, y);
			    
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right_eye_down = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right_eye_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right_eye_right = TPoint(x, y);

			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			mouth_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			mouth_right = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			mouth_up = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			mouth_down = TPoint(x, y);

			readStream.ReadL(2);//precitanie noveho riadku
			
			//nacitanie jetov
			TInt jetsize = readStream.ReadInt16L();
			CArrayFixFlat<TReal>* values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iLeftEyeJet = new (ELeave) CGaborJet();
			iLeftEyeJet->SetValuesL(values);
			
			readStream.ReadL(2);//precitanie noveho riadku
			jetsize = readStream.ReadInt16L();
			delete values;
			values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iRightEyeJet = new (ELeave) CGaborJet();
			iRightEyeJet->SetValuesL(values);
			
			readStream.ReadL(2);//precitanie noveho riadku
			jetsize = readStream.ReadInt16L();
			delete values;
			values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iNoseJet = new (ELeave) CGaborJet();
			iNoseJet->SetValuesL(values);
			
			readStream.ReadL(2);//precitanie noveho riadku
			jetsize = readStream.ReadInt16L();
			delete values;
			values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iMouthJet = new (ELeave) CGaborJet();
			iMouthJet->SetValuesL(values);
			
			
			CleanupStack::PopAndDestroy(&readStream); 
					
			//closing fileserver connection
			fileserver.Close();
			CleanupStack::PopAndDestroy(&fileserver); // destroy fs
	}

void CFaceGraph::LoadGraph(const TDesC& aFileName)
	{
			//before opening file we need a fileserver connection
			RFs fileserver;
			User::LeaveIfError(fileserver.Connect());
			CleanupClosePushL(fileserver);
		 
			RFileReadStream readStream;
			readStream.PushL();// reader on cleanup stack
			User::LeaveIfError(readStream.Open(fileserver, aFileName, EFileRead));
			
			TInt x;
			TInt y;
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			up = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right = TPoint(x, y);		  
			
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			nose_left = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			nose_right = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			left_eye_center = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			    
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			right_eye_center = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			x = readStream.ReadInt16L();
			readStream.ReadL(2);//precitanie medzery
			y = readStream.ReadInt16L();
			mouth_up = TPoint(x, y);
			
			readStream.ReadL(2);//precitanie noveho riadku
			
			//nacitanie jetov
			TInt jetsize = readStream.ReadInt16L();
			CArrayFixFlat<TReal>* values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iLeftEyeJet = new (ELeave) CGaborJet();
			iLeftEyeJet->SetValuesL(values);
			
			readStream.ReadL(2);//precitanie noveho riadku
			jetsize = readStream.ReadInt16L();
			delete values;
			values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iRightEyeJet = new (ELeave) CGaborJet();
			iRightEyeJet->SetValuesL(values);
			
			readStream.ReadL(2);//precitanie noveho riadku
			jetsize = readStream.ReadInt16L();
			delete values;
			values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iNoseJet = new (ELeave) CGaborJet();
			iNoseJet->SetValuesL(values);
			
			readStream.ReadL(2);//precitanie noveho riadku
			jetsize = readStream.ReadInt16L();
			delete values;
			values = new(ELeave) CArrayFixFlat<TReal>(jetsize);
			for(TInt i=0; i< jetsize;i++)
			{
				readStream.ReadL(2);//precitanie noveho riadku
				values->AppendL(readStream.ReadReal32L());
			}
			iMouthJet = new (ELeave) CGaborJet();
			iMouthJet->SetValuesL(values);
			
			
			CleanupStack::PopAndDestroy(&readStream); 
					
			//closing fileserver connection
			fileserver.Close();
			CleanupStack::PopAndDestroy(&fileserver); // destroy fs
	}

void CFaceGraph::SaveToFile(const TDesC& aFileName)
	{
			//before opening file we need a fileserver connection
			RFs fileserver;
			User::LeaveIfError(fileserver.Connect());
			CleanupClosePushL(fileserver);
			
			if (!BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),aFileName)) // file does not exist - create it
            {
				RFile file;
				User::LeaveIfError(file.Create(CCoeEnv::Static()->FsSession(),aFileName,EFileWrite));
				file.Close();
			}
			
			RFileWriteStream writeStream;
			writeStream.PushL();// reader on cleanup stack
			User::LeaveIfError(writeStream.Open(fileserver, aFileName, EFileWrite));
			
			writeStream.WriteInt16L(up.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(up.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(up1_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(up1_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(up1_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(up1_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(up2_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(up2_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(up2_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(up2_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(down.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(down.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(down1_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(down1_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(down1_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(down1_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();  
			
			writeStream.WriteInt16L(down2_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(down2_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(down2_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(down2_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL(); 
			
			writeStream.WriteInt16L(nose_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(nose_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(nose_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(nose_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL(); 			    
			
			writeStream.WriteInt16L(left_eye_center.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left_eye_center.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(left_eye_up.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left_eye_up.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(left_eye_down.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left_eye_down.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(left_eye_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left_eye_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			    
			writeStream.WriteInt16L(left_eye_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left_eye_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(right_eye_center.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right_eye_center.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(right_eye_up.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right_eye_up.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(right_eye_down.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right_eye_down.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(right_eye_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right_eye_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			    
			writeStream.WriteInt16L(right_eye_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right_eye_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();

			writeStream.WriteInt16L(mouth_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(mouth_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(mouth_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(mouth_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(mouth_up.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(mouth_up.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			    
			writeStream.WriteInt16L(mouth_down.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(mouth_down.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			//zapis jetov
			writeStream.WriteInt16L(iLeftEyeJet->GetSize());
			for(TInt i=0;i<iLeftEyeJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iLeftEyeJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.WriteL(_L("\n"));
			writeStream.WriteInt16L(iRightEyeJet->GetSize());
			for(TInt i=0;i<iRightEyeJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iRightEyeJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.WriteL(_L("\n"));
			writeStream.WriteInt16L(iNoseJet->GetSize());
			for(TInt i=0;i<iNoseJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iNoseJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.WriteL(_L("\n"));
			writeStream.WriteInt16L(iMouthJet->GetSize());
			for(TInt i=0;i<iMouthJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iMouthJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.Close();
						
			CleanupStack::PopAndDestroy(&writeStream); 
						
			//closing fileserver connection
			fileserver.Close();
			CleanupStack::PopAndDestroy(&fileserver); // destroy fs
	}

void CFaceGraph::SaveGraph(const TDesC& aFileName)
	{
			//before opening file we need a fileserver connection
			RFs fileserver;
			User::LeaveIfError(fileserver.Connect());
			CleanupClosePushL(fileserver);
			 
			if (!BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),aFileName)) // file does not exist - create it
			{
				RFile file;
				TInt err  = file.Create(CCoeEnv::Static()->FsSession(),aFileName,EFileWrite);
				if(err)
					return;
				file.Close();
			}
			
			RFileWriteStream writeStream;
			writeStream.PushL();// reader on cleanup stack
			TInt err  = writeStream.Open(fileserver, aFileName, EFileWrite);
			if(err)
				return;
			
			writeStream.WriteInt16L(up.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(up.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(nose_left.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(nose_left.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(nose_right.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(nose_right.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL(); 			    
			
			writeStream.WriteInt16L(left_eye_center.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(left_eye_center.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			writeStream.WriteInt16L(right_eye_center.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(right_eye_center.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();

			writeStream.WriteInt16L(mouth_up.iX);
			writeStream.WriteL(_L(" "));
			writeStream.WriteInt16L(mouth_up.iY);
			writeStream.WriteL(_L("\n"));
			writeStream.CommitL();
			
			//zapis jetov
			writeStream.WriteInt16L(iLeftEyeJet->GetSize());
			for(TInt i=0;i<iLeftEyeJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iLeftEyeJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.WriteL(_L("\n"));
			writeStream.WriteInt16L(iRightEyeJet->GetSize());
			for(TInt i=0;i<iRightEyeJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iRightEyeJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.WriteL(_L("\n"));
			writeStream.WriteInt16L(iNoseJet->GetSize());
			for(TInt i=0;i<iNoseJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iNoseJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.WriteL(_L("\n"));
			writeStream.WriteInt16L(iMouthJet->GetSize());
			for(TInt i=0;i<iMouthJet->GetSize();i++)
			{
				writeStream.WriteL(_L("\n"));
				writeStream.WriteReal32L(iMouthJet->GetValue(i));
			}
			writeStream.CommitL();
			
			writeStream.Close();
						
			CleanupStack::PopAndDestroy(&writeStream); 
						
			//closing fileserver connection
			fileserver.Close();
			CleanupStack::PopAndDestroy(&fileserver); // destroy fs
	}

void CFaceGraph::LoadFaceLine(CArea *aArea)
	{
		up = aArea->GetTop();
	    down =  aArea->GetBottom();
	    left = aArea->GetLeft();
	    right = aArea->GetRight();

	    up1_left = aArea->GetLeftBorderL(up.iY+((left.iY - up.iY)/3));
	    up1_right = aArea->GetRightBorderL(up.iY+((left.iY - up.iY)/3));
	    up2_left = aArea->GetLeftBorderL(up.iY+((left.iY - up.iY)*2/3));
	    up2_right = aArea->GetRightBorderL(up.iY+((left.iY - up.iY)*2/3));

	    down1_left = aArea->GetLeftBorderL(left.iY+((down.iY - left.iY)/3));
	    down1_right = aArea->GetRightBorderL(left.iY+((down.iY - left.iY)/3));
	    down2_left = aArea->GetLeftBorderL(left.iY+((down.iY - left.iY)*2/3));
	    down2_right = aArea->GetRightBorderL(left.iY+((down.iY - left.iY)*2/3));
	}

TInt CFaceGraph::IsSame(CFaceGraph *aGraph)
	{
		TInt failed = 0;
		TInt ok = 0;
		TInt crit = 5;

	    TReal index = 1;

	        //ziskanie pomeru grafu na zaklade sirky hlavy
	    TReal facewidth = CImageUtils::GetPointDistance(left, right);
	    TReal facewidth2 = CImageUtils::GetPointDistance(aGraph->left, aGraph->right);

	        if(Abs(facewidth - facewidth2) > crit)
	        {
	            index = facewidth / facewidth2;
	        }

	        //vzdialenost oci
	        double eyedistance = CImageUtils::GetPointDistance(left_eye_center, right_eye_center);
	        double eyedistance2 = CImageUtils::GetPointDistance(aGraph->left_eye_center, aGraph->right_eye_center);

	        if(Abs(eyedistance - index * eyedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost oci - %f"), Abs(eyedistance - index * eyedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost oci OK"));
	            ok++;
	        }

	        //vzdialenost oka od nosnej dierky
	        double lefteyenosedistance = CImageUtils::GetPointDistance(left_eye_center, nose_left);
	        double lefteyenosedistance2 = CImageUtils::GetPointDistance(aGraph->left_eye_center, aGraph->nose_left);

	        if(Abs(lefteyenosedistance - index * lefteyenosedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost laveho oka od nosnej dierky - %f"), Abs(lefteyenosedistance - index * lefteyenosedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost laveho oka od nosnej dierky OK"));
	            ok++;
	        }

	        double righteyenosedistance = CImageUtils::GetPointDistance(right_eye_center, nose_right);
	        double righteyenosedistance2 = CImageUtils::GetPointDistance(aGraph->right_eye_center, aGraph->nose_right);

	        if(Abs(righteyenosedistance - index * righteyenosedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost praveho oka od nosnej dierky - %f"), Abs(righteyenosedistance - index * righteyenosedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost praveho oka od nosnej dierky OK"));
	            ok++;
	        }

	        //vzdialenosti oka od kraje tvare
	        double lefteyefacedistance = CImageUtils::GetPointDistance(left_eye_center, left);
	        double lefteyefacedistance2 = CImageUtils::GetPointDistance(aGraph->left_eye_center, aGraph->left);

	        if(Abs(lefteyefacedistance - index * lefteyefacedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost laveho oka od kraja tvare - %f"), Abs(lefteyefacedistance - index * lefteyefacedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost laveho oka od kraja tvare OK"));
	            ok++;
	        }

	        double righteyefacedistance = CImageUtils::GetPointDistance(right_eye_center, right);
	        double righteyefacedistance2 = CImageUtils::GetPointDistance(aGraph->right_eye_center, aGraph->right);

	        if(Abs(righteyefacedistance - index * righteyefacedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost praveho oka od kraja tvare - %f"), Abs(righteyefacedistance - index * righteyefacedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost praveho oka od kraja tvare OK"));
	            ok++;
	        }

	        //vzdialenost nosnej dierky
	        double leftnosefacedistance = CImageUtils::GetPointDistance(nose_left, left);
	        double leftnosefacedistance2 = CImageUtils::GetPointDistance(aGraph->nose_left, aGraph->left);

	        if(Abs(leftnosefacedistance - index * leftnosefacedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost lavej nosnej dierky od kraja tvare - %f"), Abs(leftnosefacedistance - index * leftnosefacedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost pravej lavej dierky od kraja tvare OK"));
	            ok++;
	        }

	        double rightnosefacedistance = CImageUtils::GetPointDistance(nose_right, right);
	        double rightnosefacedistance2 = CImageUtils::GetPointDistance(aGraph->nose_right, aGraph->right);

	        if(Abs(rightnosefacedistance - index * rightnosefacedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost pravej nosnej dierky od kraja tvare - %f"), Abs(rightnosefacedistance - index * rightnosefacedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost pravej nosnej dierky od kraja tvare OK"));
	            ok++;
	        }

	        //vzdialenost nosny dierok od hornej hrany ust
	        int nosemountdistance = mouth_up.iY - Max(nose_left.iY, nose_right.iY);
	        int nosemountdistance2 = aGraph->mouth_up.iY - Max(aGraph->nose_left.iY, aGraph->nose_right.iY);

	        if(Abs(nosemountdistance - index * nosemountdistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost nosnej dierky od kraja ust - %f"), Abs(nosemountdistance - index * nosemountdistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost nosnej dierky od kraja ust OK"));
	            ok++;
	        }

	        //vzdialenost korena nosa a vrchola hlavy
	        TPoint noseroot = TPoint(left_eye_center.iX + ((right_eye_center.iX - left_eye_center.iX)/2), left_eye_center.iY + ((right_eye_center.iY - left_eye_center.iY)/2));
	        TPoint noseroot2 = TPoint(aGraph->left_eye_center.iX + ((aGraph->right_eye_center.iX - aGraph->left_eye_center.iX)/2), aGraph->left_eye_center.iY + ((aGraph->right_eye_center.iY - aGraph->left_eye_center.iY)/2));

	        double nosetopfacedistance = CImageUtils::GetPointDistance(noseroot, up);
	        double nosetopfacedistance2 = CImageUtils::GetPointDistance(noseroot2, aGraph->up);

	        if(Abs(nosetopfacedistance - index * nosetopfacedistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost korena nosa od horneho kraja tvare - %f"), Abs(nosetopfacedistance - index * nosetopfacedistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost korena nosa od horneho kraja tvare OK"));
	            ok++;
	        }

	        //vzdialenost vrchu hlavy a bokov
	        double facetopleftdistance = CImageUtils::GetPointDistance(up, left);
	        double facetopleftdistance2 = CImageUtils::GetPointDistance(aGraph->up, aGraph->left);

	        if(Abs(facetopleftdistance - index * facetopleftdistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost vrchola hlavy od laveho kraja tvare - %f"), Abs(facetopleftdistance - index * facetopleftdistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost vrchola hlavy od laveho kraja tvare OK"));
	            ok++;
	        }

	        double facetoprightdistance = CImageUtils::GetPointDistance(up, right);
	        double facetoprightdistance2 = CImageUtils::GetPointDistance(aGraph->up, aGraph->right);

	        if(Abs(facetoprightdistance - index * facetoprightdistance2) > crit)
	        {
	            RDebug::Print(_L("nesedi vzdialenost vrchola hlavy od praveho kraja tvare - %f"), Abs(facetoprightdistance - index * facetoprightdistance2));
	            failed++;
	        }
	        else
	        {
	            RDebug::Print(_L("vzdialenost vrchola hlavy od praveho kraja tvare OK"));
	            ok++;
	        }

	       

	        RDebug::Print(_L("Failed:" + failed));
	        RDebug::Print(_L("Ok:" + ok));

	        /*if(failed < 2)
	            return ETrue;
	        else
	            return EFalse;*/
	        return failed;
	}

void CFaceGraph::MakeJetsL(CNokiaCVImage *aImage)
	{
		//CGaborJet *temp = CGaborJet::NewL(aImage, left_eye_center, 3, 10, 6, 1, 4, 4);
		
		RDebug::Print(_L("Left eye center: %d %d"), left_eye_center.iX, left_eye_center.iY);
		RDebug::Print(_L("Left eye left: %d %d"), left_eye_left.iX, left_eye_left.iY);
		RDebug::Print(_L("Left eye right: %d %d"), left_eye_right.iX, left_eye_right.iY);
		RDebug::Print(_L("Right eye center: %d %d"), right_eye_center.iX, right_eye_center.iY);
		RDebug::Print(_L("mouth up: %d %d"), mouth_up.iX, mouth_up.iY);
		RDebug::Print(_L("nose left: %d %d"), nose_left.iX, nose_left.iY);
		iRightEyeJet = CGaborJet::NewL(aImage, right_eye_center,
			                                  3, 10, 6, 1, 4, 4);
		iLeftEyeJet = CGaborJet::NewL(aImage, left_eye_center,
	                                  3, 10, 6, 1, 4, 4);
	    iMouthJet = CGaborJet::NewL(aImage, mouth_up,
	    	                                  3, 10, 6, 1, 4, 4);	
	    iNoseJet = CGaborJet::NewL(aImage, nose_left,
	                                  3, 8, 6, 1, 4, 4);
	}

CGaborJet* CFaceGraph::GetGaborJet(TInt aInd)
	{
		switch(aInd)
		{
		case 1: return iLeftEyeJet;
		case 2: return iRightEyeJet;
		case 3: return iNoseJet;
		case 4: return iMouthJet;
		}
		return iLeftEyeJet;
	}

TReal CFaceGraph::GetJetSimilarity(CFaceGraph* aGraph)
	{
		TReal temp = 0;

        temp = temp + iLeftEyeJet->GetSimilarity(aGraph->GetGaborJet(1));
        temp = temp + iRightEyeJet->GetSimilarity(aGraph->GetGaborJet(2));
        temp = temp + iNoseJet->GetSimilarity(aGraph->GetGaborJet(3));
        temp = temp + iMouthJet->GetSimilarity(aGraph->GetGaborJet(4));
	   

        RDebug::Print(_L("Facegraph jet similarity: %f"), temp / 4);
        return temp / 4;
	    /*if(temp / 4 > 0.85)
	    	return ETrue;
	    else
	        return EFalse;*/
	}

void CFaceGraph::DrawFace(CNokiaCVImage *aImage)
	{
		// create a bitmap to be used off-screen
		CFbsBitmap* offScreenBitmap = aImage->Bitmap();

		// create an off-screen device and context
		CGraphicsContext* bitmapContext=NULL;
		CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL(offScreenBitmap);
		CleanupStack::PushL(bitmapDevice);
		User::LeaveIfError(bitmapDevice->CreateContext(bitmapContext));
		CleanupStack::PushL(bitmapContext);

		// draw area on the bitmap
		bitmapContext->SetBrushColor(KRgbYellow);
		bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
		TPoint a = TPoint(left.iX, down.iY);
		TPoint b = TPoint(right.iX, down.iY);
		TPoint c = TPoint(right.iX, up.iY);
		TPoint d = TPoint(left.iX, up.iY);
		bitmapContext->DrawLine(a, b);
		bitmapContext->DrawLine(b, c);
		bitmapContext->DrawLine(c, d);
		bitmapContext->DrawLine(d, a);

		// cleanup
		CleanupStack::PopAndDestroy(2);
	}

void CFaceGraph::DrawEyes(CNokiaCVImage *aImage)
	{
		// create a bitmap to be used off-screen
		CFbsBitmap* offScreenBitmap = aImage->Bitmap();

		// create an off-screen device and context
		CGraphicsContext* bitmapContext=NULL;
		CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL(offScreenBitmap);
		CleanupStack::PushL(bitmapDevice);
		User::LeaveIfError(bitmapDevice->CreateContext(bitmapContext));
		CleanupStack::PushL(bitmapContext);

		// draw area on the bitmap
		bitmapContext->SetBrushColor(KRgbYellow);
		bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
		//left eye
		TPoint a = TPoint(left_eye_left.iX, left_eye_down.iY);
		TPoint b = TPoint(left_eye_right.iX, left_eye_down.iY);
		TPoint c = TPoint(left_eye_right.iX, left_eye_up.iY);
		TPoint d = TPoint(left_eye_left.iX, left_eye_up.iY);
		bitmapContext->DrawLine(a, b);
		bitmapContext->DrawLine(b, c);
		bitmapContext->DrawLine(c, d);
		bitmapContext->DrawLine(d, a);
		bitmapContext->DrawLine(left_eye_center, left_eye_center);
		
		//right eye
		a = TPoint(right_eye_left.iX, right_eye_down.iY);
		b = TPoint(right_eye_right.iX, right_eye_down.iY);
		c = TPoint(right_eye_right.iX, right_eye_up.iY);
		d = TPoint(right_eye_left.iX, right_eye_up.iY);
		bitmapContext->DrawLine(a, b);
		bitmapContext->DrawLine(b, c);
		bitmapContext->DrawLine(c, d);
		bitmapContext->DrawLine(d, a);
		bitmapContext->DrawLine(right_eye_center, right_eye_center);

		// cleanup
		CleanupStack::PopAndDestroy(2);
	}

void CFaceGraph::DrawNose(CNokiaCVImage *aImage)
	{
		// create a bitmap to be used off-screen
		CFbsBitmap* offScreenBitmap = aImage->Bitmap();

		// create an off-screen device and context
		CGraphicsContext* bitmapContext=NULL;
		CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL(offScreenBitmap);
		CleanupStack::PushL(bitmapDevice);
		User::LeaveIfError(bitmapDevice->CreateContext(bitmapContext));
		CleanupStack::PushL(bitmapContext);

		// draw area on the bitmap
		bitmapContext->SetBrushColor(KRgbYellow);
		bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
		//left nose hole
		bitmapContext->DrawEllipse(TRect(TPoint(nose_left.iX-2, nose_left.iY-2), TPoint(nose_left.iX+3, nose_left.iY+3)));//DrawLine(nose_left, nose_left);
		
		//right eye
		bitmapContext->DrawEllipse(TRect(TPoint(nose_right.iX-2, nose_right.iY-2), TPoint(nose_right.iX+3, nose_right.iY+3)));
		//bitmapContext->DrawLine(nose_right, nose_right);

		// cleanup
		CleanupStack::PopAndDestroy(2);
	}

void CFaceGraph::DrawMouth(CNokiaCVImage *aImage)
	{
		// create a bitmap to be used off-screen
		CFbsBitmap* offScreenBitmap = aImage->Bitmap();

		// create an off-screen device and context
		CGraphicsContext* bitmapContext=NULL;
		CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL(offScreenBitmap);
		CleanupStack::PushL(bitmapDevice);
		User::LeaveIfError(bitmapDevice->CreateContext(bitmapContext));
		CleanupStack::PushL(bitmapContext);

		// draw area on the bitmap
		bitmapContext->SetBrushColor(KRgbYellow);
		bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
		TPoint a = TPoint(mouth_left.iX, mouth_down.iY);
		TPoint b = TPoint(mouth_right.iX, mouth_down.iY);
		TPoint c = TPoint(mouth_right.iX, mouth_up.iY);
		TPoint d = TPoint(mouth_left.iX, mouth_up.iY);
		bitmapContext->DrawLine(a, b);
		bitmapContext->DrawLine(b, c);
		bitmapContext->DrawLine(c, d);
		bitmapContext->DrawLine(d, a);

		// cleanup
		CleanupStack::PopAndDestroy(2);
	}

void CFaceGraph::DrawFaceGraph(CNokiaCVImage *aImage)
	{
		// create a bitmap to be used off-screen
		CFbsBitmap* offScreenBitmap = aImage->Bitmap();

		// create an off-screen device and context
		CGraphicsContext* bitmapContext=NULL;
		CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL(offScreenBitmap);
		CleanupStack::PushL(bitmapDevice);
		User::LeaveIfError(bitmapDevice->CreateContext(bitmapContext));
		CleanupStack::PushL(bitmapContext);

		// draw area on the bitmap
		bitmapContext->SetBrushColor(KRgbYellow);
		bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
		bitmapContext->DrawLine(left_eye_center, right_eye_center);
		bitmapContext->DrawLine(left_eye_center, nose_left);
		bitmapContext->DrawLine(right_eye_center, nose_right);
		bitmapContext->DrawLine(nose_left, nose_right);
		TPoint nose_middle = TPoint((nose_left.iX+(nose_right.iX-nose_left.iX)/2), (Min(nose_left.iY, nose_right.iY)+(Abs(nose_left.iY - nose_right.iY)/2)));
		bitmapContext->DrawLine(nose_middle, mouth_up);
		bitmapContext->DrawLine(nose_left, left);
		bitmapContext->DrawLine(nose_right, right);
		bitmapContext->DrawLine(left_eye_center, left);
		bitmapContext->DrawLine(right_eye_center, right);
		TPoint nose_root = TPoint((left_eye_center.iX+(right_eye_center.iX-left_eye_center.iX)/2), (Min(left_eye_center.iY, right_eye_center.iY)+ Abs(left_eye_center.iY - right_eye_center.iY)/2));
		bitmapContext->DrawLine(nose_root, up);
		bitmapContext->DrawLine(left, up);
		bitmapContext->DrawLine(right, up);

		// cleanup
		CleanupStack::PopAndDestroy(2);
	}

void CFaceGraph::PrintDebug()
	{
		for(TInt i=0;i<iRightEyeJet->GetSize();i++)
				RDebug::Print(_L("Right eye jet %d: %e"), i, iRightEyeJet->GetValue(i));
	
		for(TInt i=0;i<iLeftEyeJet->GetSize();i++)
			RDebug::Print(_L("Left eye jet %d: %e"), i, iLeftEyeJet->GetValue(i));
		
		for(TInt i=0;i<iNoseJet->GetSize();i++)
			RDebug::Print(_L("Nose jet %d: %e"), i, iNoseJet->GetValue(i));
		
		for(TInt i=0;i<iMouthJet->GetSize();i++)
			RDebug::Print(_L("Mouth jet %d: %e"), i, iMouthJet->GetValue(i));
		
	}

TBool CFaceGraph::Compare(CFaceGraph *aGraph)
	{
		TInt failed = IsSame(aGraph);
	  
	    TReal sim = GetJetSimilarity(aGraph)*100;
	    
	    if((failed <= 1 && sim > 93)  || sim > 96)
	    {
			return ETrue;
	    }
	    if(sim > 94 && failed < 3)
	    {
			return ETrue;
	    }

        return EFalse;
	}
